package com.cdtu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdtu.dto.NoticeDTO;
import com.cdtu.dto.R;
import com.cdtu.entity.Notice;
import com.cdtu.entity.User;
import com.cdtu.service.NoticeService;
import com.cdtu.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author : Sakura
 * @Date : 2023/10/28 14:00
 * @Description : TODO 用来接收景点信息的请求
 */
@RestController
@RequestMapping("/api/notice")
//解决跨域问题
@CrossOrigin
public class NoticeController {

    @Autowired
    private NoticeService noticeService;
    @Autowired
    private UserService userService;


    @GetMapping("/list/{pageNo}/{pageSize}")
    public R list(@PathVariable Integer pageNo,
                  @PathVariable Integer pageSize,
                  @RequestParam(required = false) String searchKey) {
        LambdaQueryWrapper<Notice> noticeWrapper = new LambdaQueryWrapper<>();
        noticeWrapper.like(searchKey != null && searchKey != "", Notice::getTitle, searchKey);
        Page<NoticeDTO> pageDTO = new Page<>(pageNo,pageSize);
        Page<Notice> page = noticeService.page(new Page<>(pageNo, pageSize), noticeWrapper);

        List<NoticeDTO> noticeDTOList = page.getRecords().stream().map(item -> {
            NoticeDTO dto = new NoticeDTO();
            BeanUtils.copyProperties(item, dto);
            User user = userService.getById(item.getPublishUser());
            dto.setPublishUserName(user.getName());
            return dto;
        }).collect(Collectors.toList());
        BeanUtils.copyProperties(page, pageDTO, "records");
        pageDTO.setRecords(noticeDTOList);
        return R.success(pageDTO);
    }


    @Transactional
    @PostMapping("/addOrUpdate/{uid}")
    public R noticeDetail(@RequestBody NoticeDTO dto,@PathVariable Long uid) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(dto, notice);
        if (Objects.isNull(notice.getId()) || notice.getId() == 0) {
            notice.setPublishUser(uid);
            notice.setPublishTime(new Date());
            boolean save = noticeService.save(notice);
            if (!save) {
                return R.error("添加失败，请重新尝试");
            }
            return R.success("添加成功");
        }
//        id不为Null就是修改
        boolean update = noticeService.updateById(notice);
        if (!update) {
            return R.error("修改失败，请重新尝试");
        }
        return R.success("修改成功");
    }

    @Transactional
    @PostMapping("/delete/{nid}")
    public R deleteDept(@PathVariable("nid") long nid) {
        boolean remove = noticeService.removeById(nid);
        if (!remove) {
            return R.error("删除失败，请重新尝试");
        }
        return R.success("删除成功");
    }

    @PostMapping("/detail/{nid}")
    public R detail(@PathVariable("nid") long nid) {
        Notice notice = noticeService.getById(nid);
        NoticeDTO dto = new NoticeDTO();
        User user = userService.getById(notice.getPublishUser());
        BeanUtils.copyProperties(notice, dto);
        dto.setPublishUserName(user.getName());
        return R.success(dto);
    }
}
